//
//  TF_NetworkLines.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_NetworkLines: UIView {
    var networkLinesData = NSMutableArray()
    var containerView:UIView = UIView()
    let title = UILabel()//切换线路
    let prompt = UILabel()//观看异常可手动切换视频线路
    let closeBtn = TalkfunIncreaseButton()//观看异常可手动切换视频线路
    let line = UILabel()
    let scrollView = UIScrollView()
    //线路选择block
    @objc public var networkLineBlock: ((NSInteger) -> ())?
  
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.containerView.backgroundColor = .white
        

        self.containerView.layer.cornerRadius = 5
        self.containerView.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.containerView.layer.shadowColor = UIColor .lightGray.cgColor;
        self.containerView.layer.shadowRadius = 3
        self.containerView.layer.shadowOpacity = 1
        self.addSubview(self.containerView)
        

        self.title.text = "切换线路";
        self.title.font = UIFont.PingFang(size: 17)
        self.containerView.addSubview(self.title)
        
        self.closeBtn.addTarget(self, action: #selector(closeBtnClicked), for: .touchUpInside)
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/xxx.png"), for: .normal)
        self.containerView.addSubview(self.closeBtn)
        
        self.line.backgroundColor = UIColor.gray
        self.containerView.addSubview(self.line)
      
        self.prompt.textAlignment = .center;
        self.prompt.text = "观看异常可手动切换视频线路";
        self.prompt.font = UIFont.PingFang(size: 13)
        self.containerView.addSubview(self.prompt)
        
       self.containerView.addSubview(self.scrollView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc public func networkLines(data:NSMutableArray) {
      
          self.networkLinesData = data
          
          var containerViewHeight:CGFloat = 0
          let containerViewW:CGFloat = 300
       
          let scrollViewW = containerViewW - 16 - 16
          
          if (networkLinesData.count > 0) {
             
              self.scrollView.subviews.forEach { subview in
                  subview.removeFromSuperview()
              }
              
              var height:CGFloat = (270.0 - 172.0) + (CGFloat(networkLinesData.count) / 2.0 * 50.0)
              
              let spacing :NSInteger = (networkLinesData.count%2 * 50)
              
              height = height + CGFloat(spacing)
              
              if (height > 270.0) {
                  height = 270.0
              }
              
              containerViewHeight = height;
              
              self.scrollView.backgroundColor = UIColor.clear
              self.containerView.backgroundColor = UIColor.white
              for i in 0..<networkLinesData.count
              {

                  let dict:TFPlaybackLineData = networkLinesData[i] as! TFPlaybackLineData
                  
                   let name = dict.name
                   let number = dict.current
                  
                   let btn = UIButton()
                  
                  btn.layer.cornerRadius = 3
                  btn.layer.borderColor = UIColor.lightGray.cgColor
                  btn.layer.borderWidth = 0.5
                  
                  let btnX = i%2 * (Int(scrollViewW)/2+5)
                  let btnY = i/2 * (40 + 10)
                  let btnW = scrollViewW / 2 - 5
                  let btnH = 40
                  btn.frame = CGRect(x: btnX, y: btnY, width: Int(btnW), height: btnH)
                                     
                  btn.setTitle(name, for: .normal)
                 
                  btn.titleLabel?.font = UIFont.PingFang(size: 14)
                  
                  btn.setTitleColor( UIColor.black, for: .normal)
                
               
                  btn.addTarget(self, action: #selector(networkLineBtnClicked), for: .touchUpInside)
                                     
                  btn.tag = 100+i;
                  self.scrollView.addSubview(btn)
                  
                  if (number == true ) {
                     
                      
                      btn.backgroundColor =  UIColor(red: 226/255.0, green: 237/255.0, blue: 255/255.0, alpha: 1.0)
                      
                      btn.layer.borderColor = UIColor(red: 66/255.0, green: 133/255.0, blue: 244/255.0, alpha: 1.0).cgColor
                  }
              }
            let new_scrollViewW:CGFloat = self.scrollView.frame.width
              var new_scrollViewH:CGFloat = CGFloat(networkLinesData.count)/2*50
             
              let new_scrollViewH1:NSInteger = Int(networkLinesData.count)%2*50
              
              new_scrollViewH = new_scrollViewH + CGFloat(new_scrollViewH1)
//              Int(CGFloat(networkLinesData.count))/2*50 + networkLinesData.count%2*50
              
              
            self.scrollView.contentSize = CGSize(width: new_scrollViewW, height: new_scrollViewH)
 
          }
          else
          {
              containerViewHeight = 270 - 188 + 50
          }
          
          let containerViewX = (self.frame.size.width - containerViewW)/2;
          
          self.containerView.frame = CGRect(x: containerViewX, y: (self.frame.size.height - containerViewHeight)/2, width: containerViewW, height: containerViewHeight)
          
        
          self.title.frame = CGRect(x: 16, y: 0, width: self.frame.size.width - 16 - 25, height: 36)
          
          
          self.closeBtn.frame = CGRect(x: containerViewW - 5 - 20, y: 5, width: 20, height: 20)
        
          self.line.frame = CGRect(x: 0, y: 36, width: containerViewW, height: 1)
          
          let scrollViewH = self.containerView.frame.size.height - 82 - 16
          
          self.prompt.frame = CGRect(x: 0, y: 45, width: containerViewW, height: 21)
          
          if ( scrollViewH > 0 ) {

           self.scrollView.frame = CGRect(x: 16, y: 82, width: scrollViewW, height: scrollViewH)
          }
          
          
      }
      
    @objc func closeBtnClicked(btn:UIButton)
    {
        if ((self.superview) != nil) {
            self.removeFromSuperview()
        }
    }
   @objc func networkLineBtnClicked(btn:UIButton)
   {
       
       for i in 0..<self.scrollView.subviews.count
       {
           let button = self.scrollView.subviews[i] as UIView
           button.backgroundColor = UIColor.clear
           button.layer.borderColor = UIColor.lightGray.cgColor
            
       }
      
       btn.backgroundColor = UIColor(red: 226/255.0, green: 237/255.0, blue: 255/255.0, alpha: 1.0)
       
    
       btn.layer.borderColor = UIColor(red: 66/255.0, green: 133/255.0, blue: 244/255.0, alpha: 1.0).cgColor
       
       if ((self.networkLineBlock) != nil) {
           self.networkLineBlock!( btn.tag - 100);
       }
       
       UIView.animate(withDuration: 0.5) {
           self.alpha = 0.0
       } completion: { Bool in
           self.removeFromSuperview()
       }

     
   }
                                   
                         
}
